*Table A.37: Meal and Snack treatment effects on body mass index (percentile rank) - correction for attrition
cd ""

*Import and save trimmed samples first
import excel "trimmed_samplesDD_BMI.xlsx", sheet("session3DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s3DD", replace

import excel "trimmed_samplesDD_BMI.xlsx", sheet("session4DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s4DD", replace

import excel "trimmed_samplesDD_BMI.xlsx", sheet("session5DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s5DD", replace

import excel "trimmed_samplesDD_BMI.xlsx", sheet("session6DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s6DD", replace


import excel "trimmed_samplesDD_AddedSugs.xlsx", sheet("session3DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s3DD_AS", replace

import excel "trimmed_samplesDD_AddedSugs.xlsx", sheet("session4DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s4DD_AS", replace

import excel "trimmed_samplesDD_AddedSugs.xlsx", sheet("session5DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s5DD_AS", replace

import excel "trimmed_samplesDD_AddedSugs.xlsx", sheet("session6DD") firstrow clear
keep id_nb lower_trimDD	upper_trimDD
save "trimmed_samples_s6DD_AS", replace



*Use the main dataset
use master_data_adults_child_s13456_shared.dta, clear

drop meal_control snack_control
gen meal_control=1 if treatment==2
replace meal_control=0 if treatment==1
tab meal_control

gen snack_control=1 if treatment==3
replace snack_control=0 if treatment==1
tab snack_control

drop if type_id=="X" // **Dropping newborns born during the three years of study
drop if id=="3885553B" //**error in the id number and empty data
drop if id=="3885553G" //**error in the id number and empty data
replace treatment=2 if id=="38855553B" //**retrieving the correct treatment for this id
replace treatment=2 if id=="38855553G" //**retrieving the correct treatment for this id
drop if id=="101aa22c" //**this id does not exist
drop if id=="27655663" //**outlier on many measurements


***BMI****
//gen an obesity variable//
*lab define bmi ///
*-1 "Underweight" ///	
*0 "Normal weight" ///	
*1 "Overweight" ///
*2 "Obese", replace

gen obese_var=1 if bmi_cat==2
replace obese_var=0 if obese_var==. & bmi_cat!=.

gen overw_obese=1 if bmi_cat==2 | bmi_cat==1
replace overw_obese=0 if overw_obese==. & bmi_cat!=.

***percentile in children

gen pzheight=normal(zht)
gen pzweight=normal(zwt)

*BMI
*Calculate z-scores for BMI
*Calculate z-scores for BMI
egen gendermax=max(gender), by(id_nb)
ge sex=(gendermax==1) 
replace sex=. if gendermax==.

drop zbmi pzbmi
egen zbmi = zanthro(bmi,ba,UK), xvar(months) gender(sex) gencode(male=1, female=0) ageunit(month)
gen pzbmi=normal(zbmi)

ge uwgt_child=(pzbmi<=0.02)
replace uwgt_child=. if pzbmi==.

ge normalw_child=(pzbmi>0.02 & pzbmi<0.85)
replace normalw_child=. if pzbmi==.

ge overw_child=(pzbmi>=0.85 & pzbmi<0.91)
replace overw_child=. if pzbmi==.  

ge overw_obese_child=(pzbmi>=0.85)
replace overw_obese_child=. if pzbmi==.  

ge obesew_child=(pzbmi>=0.91)
replace obesew_child=. if pzbmi==.

drop bmi_cat_child
ge bmi_cat_child=.
replace bmi_cat_child=-1 if uwgt_child==1
replace bmi_cat_child=0 if normalw_child==1
replace bmi_cat_child=1 if overw_child==1
replace bmi_cat_child=2 if obesew_child==1
drop bmi_cat_child

***macro nutrients*****
ge food15_other_veg_new=food15_other_veg
replace food15_other_veg_new=0 if food15_other_veg==. & energykcal!=.

ge food16_fruit_new=food16_fruit
replace food16_fruit_new=0 if food16_fruit==. & energykcal!=.

su food15_other_veg 
su food16_fruit 
su food15_other_veg_new 
su food16_fruit_new

*
drop portion_veg_wisp portion_fruit_wisp
gen portion_veg_wisp=food15_other_veg/80
gen portion_fruit_wisp=food16_fruit/80
gen fuit_and_veg_g=total_veg_fruit_eatwell if session==1
replace fuit_and_veg_g=total_veg_fruit_eatwell if session==3
replace fuit_and_veg_g=food15_other_veg+food16_fruit if session==4

ge FV=food15_other_veg_new+food16_fruit_new


********************************************************************************
ge meal=(treatment==2)
replace meal=. if treatment==.

ge snack=(treatment==3)
replace snack=. if treatment==.

ge session1=(session==1)
replace session1=. if session==.

ge session3=(session==3)
replace session3=. if session==.

ge session4=(session==4)
replace session4=. if session==.

ge session5=(session==5)
replace session5=. if session==.

ge session6=(session==6)
replace session6=. if session==.

ge meal_session1=meal*session1
ge meal_session3=meal*session3
ge meal_session4=meal*session4
ge meal_session5=meal*session5
ge meal_session6=meal*session6

ge snack_session1=snack*session1
ge snack_session3=snack*session3
ge snack_session4=snack*session4
ge snack_session5=snack*session5
ge snack_session6=snack*session6


set more off
********************************************************************************
xtset id_nb session
********************************************************************************
*Lee Bounds
*Percentile Rank
keep id_nb treatment pzbmi zbmi session session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 child hh_id nmesg
save temp_before_trimming, replace

*Find samples
xtreg zbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1, cluster(hh_id) fe
ta treatment if session==1 & e(sample)==1
ta treatment if session==3 & e(sample)==1
ta treatment if session==4 & e(sample)==1
ta treatment if session==5 & e(sample)==1
ta treatment if session==6 & e(sample)==1


*********************************************************************************
*Lee bounds
*BMI - ZBMI Score
*********************************************************************************
use temp_before_trimming, replace
merge m:1 id_nb using trimmed_samples_s3DD
destring lower_trimDD upper_trimDD, replace

xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & (session==1 |  session==3), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & lower_trimDD==1  & (session==1 | session==3), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & upper_trimDD==1 & (session==1 | session==3), cluster(hh_id) fe


use temp_before_trimming, replace
merge m:1 id_nb using trimmed_samples_s4DD
destring lower_trimDD upper_trimDD, replace

xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & (session==1 |  session==4), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & lower_trimDD==1  & (session==1 | session==4), cluster(hh_id) fe 
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & upper_trimDD==1 & (session==1 | session==4), cluster(hh_id) fe


use temp_before_trimming, replace
merge m:1 id_nb using trimmed_samples_s5DD
destring lower_trimDD upper_trimDD, replace

xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & (session==1 |  session==5), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & lower_trimDD==1  & (session==1 | session==5), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & upper_trimDD==1 & (session==1 | session==5), cluster(hh_id) fe


use temp_before_trimming, replace
merge m:1 id_nb using trimmed_samples_s6DD
destring lower_trimDD upper_trimDD, replace

xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & (session==1 |  session==6), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & lower_trimDD==1  & (session==1 | session==6), cluster(hh_id) fe
xtreg pzbmi session3 session4 session5 session6 meal_session3 meal_session4 meal_session5 meal_session6 snack_session3 snack_session4 snack_session5 snack_session6 if child==1 & upper_trimDD==1 & (session==1 | session==6), cluster(hh_id) fe
